/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: property_impl.h
 *
 * Purpose: property inner implementation for base library
 *
 * Developer:
 *   wen.gu , 2019-10-09
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __PANDA_SYS_INTERNAL_PROPERTY_IMPL_H__
#define __PANDA_SYS_INTERNAL_PROPERTY_IMPL_H__

#include <thread>
#include <condition_variable>

#include "panda/core/types.h"
#include "panda/sys/property.h"
#include "panda/sys/internal/system_property_impl.h"

#include "panda/sys/ipc_client_requester.h"

namespace panda
{
namespace sys
{
namespace internal_impl
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/

 /******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
#define PROP_SRV_NAME "property_service"
#define PROP_SRV_ADDR "/dev/socket/" PROP_SRV_NAME

#define PROP_SRV_SIG_NAME "property_changed"
#define PROP_SRV_METHOD_SET "set"

 /******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

class CORE_CLASS PropertyImpl
{
public:
    using AutoLock = panda::core::AutoLock;
public:
    static PropertyImpl& instance();

    ~PropertyImpl();
protected:
    PropertyImpl();
public:
    bool initialize(bool isOwner = false);

public:
    bool set(const std::string& key, const std::string& val);
    bool set(const std::string& key, int8_t val);
    bool set(const std::string& key, int16_t val);
    bool set(const std::string& key, int32_t val);
    bool set(const std::string& key, int64_t val);
    bool set(const std::string& key, uint8_t val);
    bool set(const std::string& key, uint16_t val);
    bool set(const std::string& key, uint32_t val);
    bool set(const std::string& key, uint64_t val);
    bool set(const std::string& key, float val);
    bool set(const std::string& key, double val);



    bool get(const std::string& key, std::string& val);
    bool get(const std::string& key, int8_t& val);
    bool get(const std::string& key, int16_t& val);
    bool get(const std::string& key, int32_t& val);
    bool get(const std::string& key, int64_t& val);
    bool get(const std::string& key, uint8_t& val);
    bool get(const std::string& key, uint16_t& val);
    bool get(const std::string& key, uint32_t& val);
    bool get(const std::string& key, uint64_t& val);
    bool get(const std::string& key, float& val);
    bool get(const std::string& key, double& val);


    std::string getOr(const std::string& key, const std::string& defVal);
    int8_t      getOr(const std::string& key, int8_t   defVal);
    int16_t     getOr(const std::string& key, int16_t  defVal);
    int32_t     getOr(const std::string& key, int32_t  defVal);
    int64_t     getOr(const std::string& key, int64_t  defVal);
    uint8_t     getOr(const std::string& key, uint8_t  defVal);
    uint16_t    getOr(const std::string& key, uint16_t defVal);
    uint32_t    getOr(const std::string& key, uint32_t defVal);
    uint64_t    getOr(const std::string& key, uint64_t defVal);
    float       getOr(const std::string& key, float    defVal);
    double      getOr(const std::string& key, double   defVal);

    bool foreach(void (*propFunc)(const std::string& key, const std::string& val, void* opaque), 
                 void* opaque);

    bool getPropertyList(PropertyList& proplist);

    /**
     * wait until the property change to expect value or time out.
     * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration 
     *
     */
    bool waitForProperty(const std::string& key, 
                         const std::string& expectVal, 
                         int32_t timeoutMs = -1); 

    /** 
     * wait until the property creation or time out
     * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration 
     */
    bool waitForPropertyCreation(const std::string& key, int32_t timeoutMs = -1);

    /**
     * wait until the property changed(don't care about change to what) or time out
     * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration 
     */
    bool waitForPropertyChange(const std::string& key, int32_t timeoutMs = -1);

private:
    bool sendProp(const std::string& key, const std::string& val);
private:
    bool mIsInitialized = false;
    IpcClientRequester mClient;
    std::mutex mLock;
    std::condition_variable mCond;    
};


} /** namespace internal_impl */
} /** namespace sys */
} /** namespace panda*/

#endif /** !__PANDA_SYS_INTERNAL_PROPERTY_IMPL_H__ */
